package com.yxzc.chapter05.recursive

/**
 *
 * @author wu
 * @create 2021-09-03-11:46 
 */
object Exercise01 {
  def main(args: Array[String]): Unit = {
    /**
     * 计算斐波那切数
     * 1 1 2 3 5 8 13 21 34
     */
    println(fbn(5))

    /**
     * 求函数值已知f(1) =3  f(n)=2*f(n-1)+1  请用递归求出f(n)的值
     */
    println(f(2))

    println(peach(9))
  }

  /**
   * day10 = 1;
   * day9 = (day10[1] +1) *2
   * day8 = (day9[4] +1) *2
   */

  def peach(day: Int): Int = {
    if (day == 10) {
      1
    } else {
      (peach(day+1)+1)*2
    }
  }


  /**
   * 求函数值已知f(1) =3  f(n)=2*f(n-1)+1  请用递归求出f(n)的值
   */
  def f(n1: Int): Int = {
    if (n1 == 1) {
      3
    } else {
      2 * f(n1 - 1) + 1
    }
  }


  /**
   * 斐波那契
   *
   * @param n1
   * @return
   */
  def fbn(n1: Int): Int = {
    if (n1 == 1 || n1 == 2) {
      1
    } else {
      fbn(n1 - 1) + fbn(n1 - 2)
    }
  }
}
